package com.producteev.version0.test;

import com.producteev4j.exceptions.ProducteevException;
import com.producteev4j.exceptions.ProducteevUserException;
import com.producteev4j.model.response.User;
import com.producteev4j.model.response.UserLogin;
import com.producteev4j.services.ProducteevService;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by IntelliJ IDEA.
 * User: jcarrey
 * Date: 14/05/11
 * TimeImpl: 17:07
 * To change this template use File | Settings | File Templates.
 */
public class ITTestProducteevServices extends TestCase {

    private static ProducteevService service;

    @Before
    public void setUp() {
        if (service == null) {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-test.xml");
            service = (ProducteevService) applicationContext.getBean("service");
        }
    }


    private static final String EMAIL = "juankarrey@gmail.com";
    private static final String PASSWORD = "pr0duc733v4j";

    private static String userToken;
    private static User user;
    private static Date time;

    // SIGN-UP DATA
    private static final boolean IS_SIGNUP_ACTIVE = false;
    private static final String email = "juanaka@gmail.com";
    private static final String password = "pr0duc733v4j";


    @Test
    public void testUserLogin() throws ProducteevException {
        UserLogin userLogin = service.userLogin(EMAIL, PASSWORD);
        userToken = userLogin.getToken();
    }

    @Test
    public void testGetTime() throws ProducteevException {
        time = service.getTime();
    }

    @Test
    public void testGetUser() throws ProducteevException {
        while (user == null) {  // Try to loggin, sometimes it fails...
            try {
                user = service.getUser(userToken, null);
            } catch (ProducteevException pe) {
                // relogin
                testUserLogin();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // continue
                }
            }
        }
    }


    @Test
    public void testSetDashboard() throws ProducteevException {
        if (user.getDefaultDashboard() > 0) {
            service.setDefaultDashboard(userToken, user.getDefaultDashboard());
        }
    }

    @Test
    public void testSetDashboardFailed() throws ProducteevException {
        try {
            service.setDefaultDashboard(userToken, user.getDefaultDashboard() + 1);
            Assert.fail("No error");
        } catch (ProducteevUserException pere) {
            // OK! The I dont own that dashboard
        }
    }


    @Test
    public void testGetColleagues() throws ProducteevException {
        Arrays.asList(service.getColleagues(userToken));
    }


    @Test
    public void testSignUp() throws ProducteevException {
        if (IS_SIGNUP_ACTIVE) {
            service.signupUser("producteev4j", "producteev4j", password, email, TimeZone.getDefault().getID(), null);
        }
    }

    @Test
    public void testSetOrderByType() throws ProducteevException {
        boolean failed = false;

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.DEADLINE);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.LABELS);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.MANAGER);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.PRIORITY);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.TIME_CREATED);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.TIME_LAST_CHANGE);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.TITLE);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        try {
            service.setSortByType(userToken, ProducteevService.ProducteevSortByType.WORKSPACE);
        } catch (ProducteevUserException pere) {
            failed = true;
        }

        if (failed) {
            Assert.fail();
        }
    }

    /*

    // TODO Test facebook login - need facebook oauth token
    @Test
    public void testFacebookLogin() throws ProducteevException {
        try {
            UserLogin userLogin = service.userFacebookLogin(210, "test");
            userToken = userLogin.getToken();
        } catch (ProducteevServiceException e) {
            Assert.assertTrue(e.getStatus() == HttpStatus.UNAUTHORIZED);
        }
    }*/

}
